<template>
  <!-- 数据字典 -->
  <div class="dic-list-wrap">
    <!-- 查询条件 -->
    <el-row type="flex" justify="space-between">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" @submit.native.prevent>
        <el-form-item>
          <el-input
            placeholder="请输入字典名称"
            v-model="searchForm.name"
            @keyup.enter.native="search"
            clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-select filterable @change="search" v-model="searchForm.type" placeholder="请选择字典类型">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="form-search-btn" type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="more-btns">
        <el-button type="primary" @click="editDetail(0,{})">新增</el-button>
        <el-button type="primary" @click="openUploadFile">上传</el-button>
      </div>
    </el-row>
    <!-- 数据列表 -->
    <el-table
      v-loading="loading"
      ref="dicTableList"
      :data="tableData"
      style="width: 100%">
      <el-table-column label="序号" type="index" prop="id"  width="100"></el-table-column>
      <el-table-column label="字典编码" prop="id"></el-table-column>
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="类型" prop="type">
        <template slot-scope="scope">
          <template v-if="scope.row.type==0">
            所有
          </template>
          <template v-if="scope.row.type==1">
            别名
          </template>
          <template v-if="scope.row.type==2">
            键值集合
          </template>
          <template v-if="scope.row.type==3">
            对象集合
          </template>
        </template>
      </el-table-column>
      <el-table-column label="别名" prop="alias"></el-table-column>
      <el-table-column label="字典值" prop="dictValueObj" :show-overflow-tooltip="tooltip">
          <template slot-scope="scope">
            <pre v-if="scope.row.dictValueObj.length > 0" style="overflow: hidden;">{{JSON.stringify(scope.row.dictValueObj)}}</pre>
          </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="showDetail(scope.row)" type="text" size="small">详情</el-button>
          <span class="dividing-line">|</span>
          <el-button @click="editDetail(1,scope.row)" type="text" size="small">编辑</el-button>
          <span class="dividing-line">|</span>
          <el-button @click="deleteDic(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column type="expand" width="1">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="">
                <pre style="line-height: 16px; box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);">字典值：{{JSON.stringify(JSON.parse(JSON.stringify(props.row.dictValueObj)), null, 4)}}</pre>
            </el-form-item>
            <el-form-item label="创建人：">
              <span>{{ props.row.createAccount }}</span>
            </el-form-item>
            <el-form-item label="创建时间：">
              <span>{{ props.row.createTime }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-sizes="[10, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 编辑 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="editTitle"
      width="600px"
      top='15vh'
      v-if="dialogEditInfo"
      :visible.sync="dialogEditInfo"
      @close="closeEditInfo"
      v-dialogDrag>
      <dic-info :dictInfo="editDictInfo"  :closeFun="closeEditInfo"></dic-info>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog
      :close-on-click-modal="false"
      title="上传文件"
      width="400px"
      v-if="dialogUploadFile"
      :visible.sync="dialogUploadFile"
      @close="closeUploadFile"
      v-dialogDrag>
      <file-upload :closeFun="closeUploadFile"></file-upload>
    </el-dialog>
  </div>
</template>
<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 100%;
  }
</style>
<script>
  import { getDictList, delDict } from '@/api/sysConfig'
  import DicInfo from './Dialog/DicInfo'
  import FileUpload from './Dialog/fileUpload'
  export default {
    name: 'DicMaintenance',
    // 引入子组件
    components: {
      DicInfo,
      FileUpload
    },
    data() {
      return {
        // 搜索条件
        tooltip: true,
        searchForm: {
          name: '',
          type: 0
        },
        typeList:[{"id":0,"name":"全部字典类型"},{"id":1,"name":"别名"},{"id":2,"name":"键值集合"},{"id":3,"name":"对象集合"}],
        editTitle: "编辑",
        pageLoading: false,
        loading: false,
        dialogEditInfo: false,
        dialogUploadFile: false,
        // 列表数据
        tableData: [],
        // 分页
        currentPage: 1,
        pageSize: 10,
        total: 0,
        editDictInfo: {}
      }
    },
    created() {//入口
      let that = this
      // 加载列表
      that.getTableData()
    },
    methods: {
      // 获取列表数据
      getTableData(pageIndex, pageSize) {
        let that = this
        that.loading = true
        if (pageIndex) {
          that.currentPage = pageIndex
        }
        if (pageSize) {
          that.pageSize = pageSize
        }
        let parames = {
          name: that.searchForm.name.trim(),
          pageSize: that.pageSize,
          pageIndex: that.currentPage - 1,
          type: that.searchForm.type
        }
        // 获取列表信息
        getDictList(parames).then(res => {
          that.tableData = res.data
          that.total = res.total
          that.loading = false
        })
      },
      search() {
        this.getTableData(1)
      },
      handleSizeChange(pageSize) {
        this.getTableData(1, pageSize)
      },
      handleCurrentChange(currentPage) {
        this.getTableData(currentPage)
      },
      // 删除
      deleteDic(row) {
        let that = this
        if (row) {
          that.$confirm('确定要删除吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            roundButton: true
          }).then(() => {
            // 调用删除接口
            delDict({
              delCode: row.id
            }).then(res => {
              if( res.status === 200) {
                that.$message({
                  type: 'success',
                  message: '操作成功!'
                })
                // 刷新列表
                that.search()
              }else{
                that.$message.error(res.info)
              }
            })
          }).catch(() => {
          })
        }
      },
      //详情
      showDetail(row) {
        let that = this

        this.$refs.dicTableList.toggleRowExpansion(row)
      },
      // 编辑
      editDetail(type, row) {
        let that = this
        if(type==0){
          that.editTitle = "新增"
        }else{
          this.editTitle = "编辑"
        }

        if(row) {
          that.editDictInfo = row
          that.dialogEditInfo = true
        }
      },
      // 关闭编辑弹框
      closeEditInfo(flag) {
        let that = this
        that.dialogEditInfo = false
        if(flag) {
          this.getTableData(this.editDictInfo.id?'':1)
        }
      },

      //上传文件
      openUploadFile() {
        let that = this
        that.dialogUploadFile = true
      },
      // 关闭编辑弹框
      closeUploadFile() {
        let that = this
        that.dialogUploadFile = false
      },
    }

  }
</script>
